package org.study.swing.study01.table;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.Vector;

/**
 * 主界面组件布局
 * 参考 https://blog.csdn.net/m0_61495539/article/details/134908536
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月12日
 */
public class TablePageDemo02 extends JFrame {
    public static void main(String[] args) {
        new TablePageDemo02();
    }

    // 北边的面板
    JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JButton addButton = new JButton("添加");
    JButton updateButton = new JButton("修改");
    JButton deleteButton = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton queryButton = new JButton("查询");
    // 南边的面板
    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton preButton = new JButton("上一页");
    JButton nextButton = new JButton("下一页");
    // 首先实例化对象
    MainViewTable mainViewTable = new MainViewTable();

    public TablePageDemo02() {
        Container contentPane = super.getContentPane();
        this.layoutNorth(contentPane);
        this.layoutCenter(contentPane);
        this.layoutSouth(contentPane);

        super.setTitle("Java Swing 表格分页示例");
        super.setBounds(100, 100, 1000, 400);
        // 将窗口居中显示在屏幕上
        super.setLocationRelativeTo(null);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setVisible(true);
    }

    private void layoutNorth(Container contentPane) {
        northPanel.add(addButton);
        northPanel.add(updateButton);
        northPanel.add(deleteButton);
        northPanel.add(searchTxt);
        northPanel.add(queryButton);
        contentPane.add(northPanel, BorderLayout.NORTH);
    }

    private void layoutSouth(Container contentPane) {
        southPanel.add(preButton);
        southPanel.add(nextButton);
        contentPane.add(southPanel, BorderLayout.SOUTH);
    }

    private void layoutCenter(Container contentPane) {
        // 测试数据
        Vector<Vector<Object>> data = new Vector<>();
        Vector<Object> rowVector1 = new Vector<>();
        rowVector1.addElement("1");
        rowVector1.addElement("张三");
        rowVector1.addElement("2112001136");
        rowVector1.addElement("江苏");
        rowVector1.addElement("89");
        rowVector1.addElement("92");
        rowVector1.addElement("80");
        rowVector1.addElement("261");

        Vector<Object> rowVector2 = new Vector<>();
        rowVector2.addElement("2");
        rowVector2.addElement("李四");
        rowVector2.addElement("2112001137");
        rowVector2.addElement("北京");
        rowVector2.addElement("89");
        rowVector2.addElement("92");
        rowVector2.addElement("85");
        rowVector2.addElement("266");

        data.addElement(rowVector1);
        data.addElement(rowVector2);

        MainViewTableModel mainViewTableModel = MainViewTableModel.assembleModel(data);
        mainViewTable.setModel(mainViewTableModel);
        mainViewTable.renderRule();
        JScrollPane jScrollPane = new JScrollPane(mainViewTable);
        contentPane.add(jScrollPane, BorderLayout.CENTER);
    }

    static class MainViewTableModel extends DefaultTableModel {
        static Vector<String> column = new Vector<>() {{
            this.addElement("编号");
            this.addElement("姓名");
            this.addElement("学号");
            this.addElement("家乡");
            this.addElement("语文");
            this.addElement("数学");
            this.addElement("英语");
            this.addElement("总分");
        }};

        public MainViewTableModel() {
            super(null, column);
        }

        // 饿汉式
        private static MainViewTableModel mainViewTableModel = new MainViewTableModel();

        public static MainViewTableModel assembleModel(Vector<Vector<Object>> data) {
            // 组装数据  -> 肯定有返回对象，即要设置返回类型
            mainViewTableModel.setDataVector(data, column);
            return mainViewTableModel;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        // 添加get方法 获取列名
        public static Vector<String> getColumn() {
            return column;
        }
    }

    static class MainViewTable extends JTable {
        public MainViewTable() {
            // 设置表头的样式
            JTableHeader tableHeader = getTableHeader();
            tableHeader.setFont(new Font("微软雅黑", Font.BOLD, 18));
            tableHeader.setForeground(Color.red);
            // 设置表格体的样式
            setFont(new Font("微软雅黑", Font.PLAIN, 14));
            setForeground(Color.black);
            setGridColor(Color.black);  // 设置表格线颜色
            setRowHeight(30);  // 设置行高
            // 设置多行选择
            getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        }

        public void renderRule() {
            // 设置表格列的渲染方式
            Vector<String> column = MainViewTableModel.getColumn();
            MainViewCellRender mainViewCellRender = new MainViewCellRender();
            for (int i = 0; i < column.size(); i++) {
                TableColumn c = getColumn(column.get(i));
                c.setCellRenderer(mainViewCellRender);
                if (i == 0) {
                    c.setPreferredWidth(10);
                    c.setResizable(false);
                }
            }
        }
    }

    static class MainViewCellRender extends DefaultTableCellRenderer {
        // 在每一行每一列显示之前都会调用
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (row % 2 == 0) {
                setBackground(Color.LIGHT_GRAY);
            } else {
                setBackground(Color.WHITE);
            }
            // 设置水平居中
            setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    }
}
